<?php
class Bll_Flow_UserRenew extends BLL {
    /**
     * 写入记录
     * @param array $data
     * @return number
     */
    public function insert_renew($data){
        $status = 0;
        if(empty($data['flow_id']) || empty($data['job_id'])){
            return $status;
        }
        return $this->get_user_renew_dao()->insert($data);
    }
    public function find_by_ids($flow_ids){
        $return = array();
        if( empty($flow_ids) ){
            return $return;
        }
        $_where["flow_id IN(".implode(',',$flow_ids).") AND 1=?"]=1;
        return $this->get_user_renew_dao()->find_short_assoc($_where, "", 0);
    }
    public function update_by_dimission($uid){
        if(empty($uid)){
            return false;
        }
        $_where = array(
                'user_id'=>$uid,
                'apply_status'=>1
        );
        $has = $this->get_user_renew_dao()->find($_where,'',0);
        $apply_bll = new Bll_Flow_ApplyRelation();
        $log_bll = new Bll_Flow_OperateLog();
        $flow_bll = new Bll_Flow_Info();
        if(!empty($has)){
            $flow_status = Const_Flow::FLOW_STATUS_EFFECT;
            foreach($has as $val){
                $this->get_user_renew_dao()->update(array('apply_status'=>$flow_status),array('flow_id'=>$val['flow_id']));
                $apply_bll->update_by_flow($val['flow_id']);

                if(empty($val['super_opinion'])){//说明领导没有操作过 write log
                    $flow_status = Const_Flow::FLOW_STATUS_SYSTEM;
                    //写入operator_log表，标记为系统处理
                    $log_data = array(
                            'user_id'      => $val['user_id'],
                            'user_name'    => $val['user_name'],
                            'opt_user_id'  => $val['opt_user_id'],//system
                            'role_type'    => Const_Flow::ROLE_SUPERIOR,//领导同意
                            'work_city_id' => $val['work_city_id'],
                            'flow_id'      => $val['flow_id'],
                            'flow_opt'     => Const_Flow::PHASE_61,
                            'flow_stage'   => Const_Flow::FLOW_RENEW_2,
                            'flow_type'    => Const_Flow::FLOW_TYPE_RENEW,
                            'effect_date'  => $val['effect_date'],
                    );
                    $log_bll->save_log($log_data);
                }
                $_data = array(
                        'flow_stage'=>Const_Flow::FLOW_RENEW_2,
                        'flow_opt'=>Const_Flow::PHASE_61,
                        'now_status'=>$flow_status
                );
                $flow_bll->update_flow_data($_data,array('flow_id'=>$val['flow_id']));
            }
        }
        return true;
    }
    /**
     * 获取User_renew dao
     * @return dao
     */
    private function get_user_renew_dao(){
        return $this->get_dao('Dao_Flow_UserRenew');
    }



    /**
     * 获取一条UserRegular记录
     * @param unknown_type $flow_id
     * @return multitype:|Ambigous <multitype:, mixed>
     */
    public function get_user_renew_by_flow_id($flow_id){
        $return = array();
        if( empty($flow_id) ){
            return $return;
        }

        $res =  $this->get_user_renew_dao()->find_by_id($flow_id);
        $res['effect_date'] = $this->format_time($res['effect_date']);
        return $res;
    }


    /**
     * 更新user_renew表，
     * @param unknown_type $post post数组
     * @return boolean
     */
    public function update_user_renew($id, $data){
        if(empty($id) || empty($data)){
            return FALSE;
        }
        $affected_row_count = $this->get_user_renew_dao()->update_by_id($id,$data);
        return $affected_row_count ? TRUE : FALSE ;
    }


    private function format_time( $time ){
        return date('Y-m-d',strtotime($time));
    }
}